package com.bdcom.autotest.ui.snp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.util.CollectionUtils;

import com.bdcom.autotest.interaction.socket.DataUtil;
import com.bdcom.autotest.interaction.socket.DevSocket;
import com.bdcom.autotest.interaction.socket.bean.BasePkg;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.ui.gif.ImgFactory;
import com.bdcom.autotest.util.UIUtil;

public class PanelForSetPort extends AbsPanel {
	JTextField slotNum1;
	JTextField portNum1;
	JTextField slotNum2;
	JTextField portNum2;
	JTextField slotNum3;
	JTextField portNum3;
	JTextField slotNum4;
	JTextField portNum4;
	
	public PanelForSetPort() {
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
		JPanel p1 = UIUtil.createPanel(); 
//		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		JLabel jLabel = new JLabel("万兆口");
		jLabel.setFont(font2);
		p1.add(jLabel);
		p1.setPreferredSize(new Dimension(280, 30));
		JPanel p2 = UIUtil.createPanel(); 
		JLabel label1 = new JLabel("端口1");
		label1.setFont(font2);
		JLabel slot1 = new JLabel("槽位号:");
		slot1.setFont(font2);
		slotNum1 = new JTextField(2);
		slotNum1.setFont(font2);
		slotNum1.setText("0");
		JLabel port1 = new JLabel("端口号:");
		port1.setFont(font2);
		portNum1 = new JTextField(2);
		portNum1.setFont(font2);
		portNum1.setText("0");
		p2.add(label1);
		p2.add(slot1);
		p2.add(slotNum1);
		p2.add(port1);
		p2.add(portNum1);
		
		JPanel p3 = UIUtil.createPanel(); 
		JLabel label2 = new JLabel("端口2");
		label2.setFont(font2);
		JLabel slot2 = new JLabel("槽位号:");
		slot2.setFont(font2);
		slotNum2 = new JTextField(2);
		slotNum2.setFont(font2);
		slotNum2.setText("0");
		JLabel port2 = new JLabel("端口号:");
		port2.setFont(font2);
		portNum2 = new JTextField(2);
		portNum2.setFont(font2);
		portNum2.setText("0");
		p3.add(label2);
		p3.add(slot2);
		p3.add(slotNum2);
		p3.add(port2);
		p3.add(portNum2);
			
		JPanel p4 = UIUtil.createPanel(); 
//		p4.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel jLabel2 = new JLabel("千兆口");
		jLabel2.setFont(font2);
		p4.setPreferredSize(new Dimension(280, 30));
		p4.add(jLabel2);
		
		JPanel p5 = UIUtil.createPanel(); 
		JLabel label3 = new JLabel("端口3");
		label3.setFont(font2);
		JLabel slot3 = new JLabel("槽位号:");
		slot3.setFont(font2);
		slotNum3 = new JTextField(2);
		slotNum3.setFont(font2);
		slotNum3.setText("0");
		JLabel port3 = new JLabel("端口号:");
		port3.setFont(font2);
		portNum3 = new JTextField(2);
		portNum3.setFont(font2);
		portNum3.setText("0");
		p5.add(label3);
		p5.add(slot3);
		p5.add(slotNum3);
		p5.add(port3);
		p5.add(portNum3);
		
		JPanel p6 = UIUtil.createPanel(); 
		JLabel label4 = new JLabel("端口4");
		label4.setFont(font2);
		JLabel slot4 = new JLabel("槽位号:");
		slot4.setFont(font2);
		slotNum4 = new JTextField(2);
		slotNum4.setFont(font2);
		slotNum4.setText("0");
		JLabel port4 = new JLabel("端口号:");
		port4.setFont(font2);
		portNum4 = new JTextField(2);
		portNum4.setFont(font2);
		portNum4.setText("0");
		p6.add(label4);
		p6.add(slot4);
		p6.add(slotNum4);
		p6.add(port4);
		p6.add(portNum4);
		this.add(p1);
		this.add(p2);
		this.add(p3);
		this.add(p4);
		this.add(p5);
		this.add(p6);
		
		JPanel p7 = UIUtil.createPanel(); 
		JButton btn1 = new JButton("确认");
		btn1.setFont(font2);
		btn1.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				writePortConfig();
			}
		});
//		JButton btn2 = new JButton("取消");
//		btn2.setFont(font2);
//		btn2.addActionListener( new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//			}
//		});
		p7.add(btn1);
//		p7.add(btn2);
		p7.setLayout(new FlowLayout(FlowLayout.CENTER ) );
		this.add(p7);
	}
	
	public void loadPortConfig(){
		List<String> strList = readTxt("conf/portConfig.txt");
		slotNum1.setText(strList.get(0));
		portNum1.setText(strList.get(1));
		slotNum2.setText(strList.get(2));
		portNum2.setText(strList.get(3));
		slotNum3.setText(strList.get(4));
		portNum3.setText(strList.get(5));
		slotNum4.setText(strList.get(6));
		portNum4.setText(strList.get(7));
	}
	
	public void writePortConfig(){
		String content = slotNum1.getText()+"\r\n";
		content += portNum1.getText()+"\r\n";
		content += slotNum2.getText()+"\r\n";
		content += portNum2.getText()+"\r\n";
		content += slotNum3.getText()+"\r\n";
		content += portNum3.getText()+"\r\n";
		content += slotNum4.getText()+"\r\n";
		content += portNum4.getText()+"\r\n";
		writeTxt("conf/portConfig.txt",content);
	}

	public List<String> readTxt(String txtPath) {
        File file = new File(txtPath);
        if(file.isFile() && file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                List<String> strList = new ArrayList();
                String text = null;
                while((text = bufferedReader.readLine()) != null){
                	strList.add(text);
                }
                return strList;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    

    /**使用FileOutputStream来写入txt文件
     * @param txtPath txt文件路径
     * @param content 需要写入的文本
     */
    public void writeTxt(String txtPath,String content){    
       FileOutputStream fileOutputStream = null;
       File file = new File(txtPath);
       try {
           if(file.exists()){
               //判断文件是否存在，如果不存在就新建一个txt
               file.createNewFile();
           }else{
        	   file.delete();
        	   file.createNewFile();
           }
           fileOutputStream = new FileOutputStream(file);
           fileOutputStream.write(content.getBytes());
           fileOutputStream.flush();
           fileOutputStream.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
}
